package cn.com.git.workflow.dao.impl;

import cn.com.git.common.base.BaseDaoImpl;
import cn.com.git.workflow.dao.WorkFlowDAO;
import cn.com.git.workflow.dao.mapper.WorkFlowMapper;
import cn.com.git.workflow.dao.model.WorkFlow;
import cn.com.git.workflow.dao.model.WorkFlowExample;
import cn.com.git.workflow.service.query.WorkFlowListQuery;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
  * WorkFlowDAO接口
  * @author zhangzhenfu
  * @Description
  * @date 2020/4/13
*/
@Repository
public class WorkFlowDAOImpl extends BaseDaoImpl<WorkFlowMapper,WorkFlow,WorkFlowExample,WorkFlowListQuery,Integer> implements WorkFlowDAO {

    @Autowired
    private WorkFlowMapper mapper;

    @Override
    protected WorkFlowMapper getMapper() {
        return mapper;
    }

    @Override
    public WorkFlowExample analysisLimit(WorkFlowListQuery query) {
        WorkFlowExample example = new WorkFlowExample();
        WorkFlowExample.Criteria c =  example.createCriteria();
        if(query.getId()!=null){
            c.andIdEqualTo(query.getId());
        }
        if(query.getIdList()!=null && query.getIdList().size()>0){
            List<Integer> idList = query.getIdList();
            idList = idList.stream().distinct().collect(Collectors.toList());
            c.andIdIn(idList);
        }
        if(query.getCurrentNode()!=null){
            c.andCurrentNodeEqualTo(query.getCurrentNode());
        }
        if(query.getType()!=null){
            c.andTypeEqualTo(query.getType());
        }
        return example;
    }

    @Override
    public WorkFlow selectById(Integer id) {
        return super.selectByPrimaryKey(id);
    }

    @Override
    public WorkFlow selectOne(WorkFlowListQuery query) {
        WorkFlowExample example = analysisLimit(query);
        //TODO
        List<WorkFlow> list =   Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
            if(list.size()>0){
                return list.get(0);
            }
        return null;
    }

    @Override
    public List<WorkFlow> selectList(WorkFlowListQuery query) {
        WorkFlowExample example = analysisLimit(query);
        return Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
    }

    @Override
    public int countLimit(WorkFlowListQuery query) {
        WorkFlowExample example = analysisLimit(query);
        return countByExample(example);
    }

    @Override
    public Integer insertOne(WorkFlow record) {
        super.insertSelective(record);
        return record.getId();
    }

    @Override
    public List<WorkFlow> insertList(List<WorkFlow> list) {
        Optional.ofNullable(list).orElse(Lists.newArrayList()).forEach(s->{
            super.insertSelective(s);
        });
        return list;
    }

    @Override
    public Integer updateOne(WorkFlow record) {
        return super.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer updateLimit(WorkFlow record, WorkFlowListQuery query) {
        WorkFlowExample example = analysisLimit(query);
        return super.updateByExampleSelective(record,example);
    }

    @Override
    public Integer deleteOne(Integer id) {
        return super.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteLimit(WorkFlowListQuery query) {
        WorkFlowExample example = analysisLimit(query);
        return super.deleteByExample(example);
    }

}